package com.baizhi.controller;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.ICaptcha;
import com.baizhi.annotation.LogAnnotation;
import com.baizhi.entity.Admin;
import com.baizhi.service.AdminRoleService;
import com.baizhi.service.AdminService;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("admin")
public class AdminController {

    @Autowired
    private AdminService adminService;
    @Autowired
    private AdminRoleService adminRoleService;

    //登录
//    @ResponseBody
    @LogAnnotation(content = "登录",type = "login")
    @RequestMapping("login")
    public String login(Admin admin, String code){
        Session session = SecurityUtils.getSubject().getSession();

        Map map = new HashMap();
        ICaptcha captcha = (ICaptcha) session.getAttribute("captcha");
        System.out.println("code = " + code);
        System.out.println("captcha.getCode() = " + captcha.getCode());
        if (!captcha.verify(code)){
            map.put("isLogin",false);
            session.setAttribute("msg","验证码错误");
            map.put("msg","验证码错误");
            return "redirect:/login.jsp";
        }

        //将获取到的数据封装到令牌中
        UsernamePasswordToken token = new UsernamePasswordToken(admin.getUsername(), admin.getPassword());
        //获取subject，调用登录方法
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);

            boolean authenticated = subject.isAuthenticated();
            if (authenticated){
                boolean admin1 = subject.hasRole("admin");
                if (admin1){
                    System.out.println("拥有管理员权限");
                }else {
                    System.out.println("meiyou");
                }

                boolean permitted = subject.isPermitted("banner:update");
                if (permitted){
                    System.out.println("可以更新轮播图");
                }else {
                    System.out.println("不可以更新轮播图");
                }
            }

            map.put("isLogin",true);
//            登录成功
            Admin admin1 = adminService.showByName(admin.getUsername());
            session.setAttribute("admin",admin1);
            return "redirect:/main.jsp";
        } catch (AuthenticationException e) {
//            登录失败
            map.put("isLogin",false);
            session.setAttribute("msg","账号或密码错误");
            map.put("msg","账号或密码错误");
            return "redirect:/login.jsp";
        }


    }

    /*    //登录
//    @ResponseBody
    @LogAnnotation(content = "登录",type = "login")
    @RequestMapping("login")
    public String login(Admin admin, String code, HttpSession session){
        Map map = new HashMap();
        ICaptcha captcha = (ICaptcha) session.getAttribute("captcha");
        System.out.println("code = " + code);
        System.out.println("captcha.getCode() = " + captcha.getCode());
        if (!captcha.verify(code)){
            map.put("isLogin",false);
            session.setAttribute("msg","验证码错误");
            map.put("msg","验证码错误");
            return "redirect:/login12.jsp";
        }

        Admin admin1 = adminService.showByName(admin.getUsername());
        System.out.println("admin1 = " + admin1);
        if (admin1 != null && admin.getPassword().equals(admin1.getPassword())){
            map.put("isLogin",true);
            session.setAttribute("msg","");
            session.setAttribute("admin",admin1);
            return "redirect:/main.jsp";
        }else {
            map.put("isLogin",false);
            session.setAttribute("msg","账号或密码错误");
            map.put("msg","账号或密码错误");
            return "redirect:/login12.jsp";
        }

    }*/


    //验证码
    @RequestMapping("getCaptcha")
    public void getCaptcha(HttpSession session, HttpServletResponse response) throws IOException {
        //定义图形验证码的长和宽
        ICaptcha captcha = CaptchaUtil.createLineCaptcha(200, 100);
        //获取验证码内容
        System.out.println(captcha.getCode());
        //将验证码对象放在session中
        session.setAttribute("captcha",captcha);
        //通过流输出
        captcha.write(response.getOutputStream());
    }

    //展示
    @LogAnnotation(content = "查询管理员")
    @ResponseBody
    @RequestMapping("show")
    public Map show(int page,int limit,String username){
        Map map = new HashMap();
        PageInfo<Admin> pageInfo = adminService.showAll(page, limit, username);
        map.put("code",0);
        map.put("msg","");
        map.put("count",pageInfo.getTotal());
        map.put("data",pageInfo.getList());
        return map;
    }

    //删除
    @LogAnnotation(content = "删除管理员账号",type = "delete")
    @ResponseBody
    @RequestMapping("delete")
    public Map delete(int id){
        Map map = new HashMap();
        try {
            adminService.remove(id);
            map.put("isDelete",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isDelete",false);
        }
        return map;
    }

    //添加
    @LogAnnotation(content = "添加管理员",type = "insert")
    @ResponseBody
    @RequestMapping("add")
    public Map add(Admin admin,Integer roleId){
        Map map = new HashMap();
        try {
            adminService.add(admin);
            adminRoleService.add(admin.getId(),roleId);
            map.put("isAdd",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isAdd",false);
        }
        return map;
    }

    //更新
    @LogAnnotation(content = "更新了管理员账号",type = "update")
    @ResponseBody
    @RequestMapping("update")
    public Map update(Admin admin){
        Map map = new HashMap();
        try {
            adminService.update(admin);
            map.put("isUpdate",true);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("isUpdate",false);
        }
        return map;
    }

    //单个查询
    @LogAnnotation(content = "单个查询管理员账户",type = "selectOne")
    @ResponseBody
    @RequestMapping("selectOne")
    public Admin selectOne(Integer id){
        Admin admin = adminService.showById(id);
        return admin;
    }


}















